<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form"  ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="用车标题" prop="title">{{ form.title }}</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="车辆类型" prop="carType">{{ form.typeName }}</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="用车人" prop="usedUser">{{ form.usedUserName }}</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="用车事由" prop="cause">{{ form.cause }}</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="出发地点" prop="sourceAddress">{{ form.sourceAddress }}</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="目的地" prop="destination">{{ form.destination }}</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="大约里程" prop="mileage">{{ form.mileage }}/公里</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否需要油卡" prop="card">{{ form.card == '0' ? '不需要' : '需要' }}</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">{{ form.beginTime }}</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">{{ form.endTime }}</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="是否需要司机" prop="driver">{{ form.driver == '0' ? '不需要' : '需要' }}</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="随车人员" prop="passenger">{{ form.passenger }}/人</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明" prop="remark">{{ form.remark }}</el-form-item>
		</el-form>
		<el-divider>审批意见</el-divider>
		<el-form :model="resForm" :rules="resRules" ref="dialogForm1" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="指定司机" prop="targetDriver">
						<sc-select v-model="resForm.targetDriver" :apiObj="this.$API.office.vehicle.getVehicleDriverListForSelect" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="发放油卡" prop="oilCard">
						<sc-select v-model="resForm.oilCard" :apiObj="this.$API.office.vehicle.getCanUsedOilCardList" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="指派车辆" prop="vehicleId">
						<sc-select v-model="resForm.vehicleId" :params="{carType:form.carType}" :apiObj="this.$API.office.vehicle.getCanUsedVehicleList" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="审批意见" prop="status">
						<el-radio-group v-model="resForm.status">
							<el-radio-button label="1">同意</el-radio-button>
							<el-radio-button label="2">不同意</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";

export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '车辆使用审批',
			},
			vehicleList: [],
			oilCardList: [],
			resForm: {
				applyId: '',
				targetDriver: '',
				oilCard: '',
				vehicleId: '',
				status: '1',
			},
			form: {
				applyId: '',
				carType: '',
				typeName: '',
				title: '',
				usedUser: '',
				usedUserName: '',
				cause: '',
				beginTime: '',
				endTime: '',
				sourceAddress: '',
				destination: '',
				mileage: 0,
				card: '0',
				driver: '0',
				optUser: '',
				passenger: '',
				remark: ''
			},
			resRules: {
				vehicleId: [
					{required: true, message: '请指定车辆', trigger: 'blur'}
				],
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		async submit() {
			let res;
			if(this.resForm.status=="1")
			{
				this.$refs.dialogForm1.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						res = await this.$API.office.vehicle.setApprovalStatus.post(this.resForm);
						this.isSaveing = false;
						if (res.code == 200) {
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			}else
			{
				this.isSaveing = true;
				res = await this.$API.office.vehicle.setApprovalStatus.post(this.resForm);
				this.isSaveing = false;
				if (res.code == 200) {
					this.$emit('success')
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}
		},
		setData(data) {
			this.form.applyId = data.applyId
			this.resForm.applyId = data.applyId
			this.form.typeName = data.typeName
			this.form.carType = data.carType
			this.form.title = data.title
			this.form.usedUser = data.usedUser
			this.form.usedUserName = data.usedUserName
			this.form.cause = data.cause
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.sourceAddress = data.sourceAddress
			this.form.destination = data.destination
			this.form.mileage = data.mileage
			this.form.card = data.card
			this.form.driver = data.driver
			this.form.optUser = data.optUser
			this.form.passenger = data.passenger
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
